package com.bluesea.report.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.bluesea.report.entity.Customer;
import com.bluesea.report.service.customer.CustomerService;


/**
 * 控制层调
 * @ClassName: CustomerController
 * @author 李爽
 * @date 2017年8月16日 下午6:50:58
 * @version V1.0
 */

@Controller
@RequestMapping("customer")
public class CustomerController {
	
    @SuppressWarnings("unused")
	@Autowired
    private HttpServletRequest req; 
    @SuppressWarnings("unused")
	@Autowired
    private HttpServletResponse res; 
    @Resource//将CustomerService类注入
	private CustomerService service; 
    /**
     * 查询全部
     * @Title: getAll
     * @return String    返回类型
     */
	@RequestMapping(value="",method=RequestMethod.GET)
	@ResponseBody
	public String getAll(){
		List<Customer> customers = service.getAll(); //调service里的查询所有的方法
		return JSONArray.toJSONString(customers);

	}
	
	/**
	 * @Title: queryAll
	 * @Description: TODO关联查询
	 * @param @return    设定文件
	 * @return String    返回类型
	 * @author 李爽
	 * @date 2017年8月29日 下午5:29:57
	 * @version V1.0
	 * @throws
	 */
	@RequestMapping(value="/query",method=RequestMethod.GET)
	@ResponseBody
	public String queryAll(){
		List<Object> customers = service.queryAll();
		return JSONArray.toJSONString(customers);

	}
	
	/**
	 * 条件查询
	 * @Title: search
	 * @param @param customer
	 * @return String    返回类型
	 */
	@RequestMapping(value="/search",method=RequestMethod.POST)
	@ResponseBody
	public String search(@RequestBody Customer customer){
		List<Customer> customers = null;//调service里的条件查询的方法
		try {
			//调用Service层业务处理
			customers = service.search(customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONArray.toJSONString(customers);
	}
	
//	@RequestMapping(value="/delete/{id}",method=RequestMethod.DELETE)
//	@ResponseBody
//	public String remove(@PathVariable("id") Integer id){
//		Integer remove =service.remove(id); //调service里的删除方法
//		List<Customer> customers = service.getAll();
//		return JSONArray.toJSONString(customers);
//	}
	
	/**
	 * 删除
	 * @Title: search
	 * @param @param customer
	 * @return String    返回类型
	 */
	@RequestMapping(value="",method=RequestMethod.DELETE)
	@ResponseBody
	public String remove(@RequestBody Integer[] ids){
		String success="";
		try {
    		//调用Service层业务处理
			service.deleteBatch(ids);
    		success = "true";
		} catch (Exception e) {
			e.printStackTrace();
			success = "false";
		}
		return JSONArray.toJSONString(success);	
	}

	/**
	 * 增加
	 * @Title: search
	 * @param @param customer
	 * @return String    返回类型
	 */
	@RequestMapping(value="",method=RequestMethod.POST)
	@ResponseBody
	public String create(@RequestBody Map<String, Object> map){
		String success = "true";
		service.create(map); //调service里的增加方法
		List<Object> customers = service.queryAll();
		System.out.println(customers);
		return JSONArray.toJSONString(customers);
	}

	/**
	 * 修改
	 * @Title: search
	 * @param @param customer
	 * @return String    返回类型
	 */
	@RequestMapping(value="",method=RequestMethod.PUT)
	@ResponseBody
	public String modify(@RequestBody Map<String, Object> map){
//		System.out.println("controller"+cust);
		String success="";
		try {
    		service.modify(map);
    		success = "true";
		} catch (Exception e) {
			e.printStackTrace();
			success = "false";
		}
		return JSONArray.toJSONString(success);
	}
	
	/**
	 * 批量撤户
	 */
	@RequestMapping(value="/invalid",method=RequestMethod.PUT)
	@ResponseBody
	public String invalid(@RequestBody Integer[] id) {
    	
    	String success = "";
    	try {
			//调用Service层业务处理
    		service.invalid(id);
    		success = "true";
		} catch (Exception e) {
			e.printStackTrace();
			success = "false";
		}
		return JSONArray.toJSONString(success);
    };
}
